package org.ns.tomdog.classloader;

import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WebAppClassLoader extends LocalClassLoader {

    private Map<String, Class<?>> classMap = new ConcurrentHashMap<>();
    private CommonClassLoader commonClassLoader;

    public WebAppClassLoader(String apppath,  Map<String, Class<?>> classMap, CommonClassLoader commonClassLoader) {
        super(commonClassLoader, new ClassPath(new File(apppath, "classes"), new File(apppath, "lib")), classMap);
        this.commonClassLoader = commonClassLoader;
    }

    @Override
    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
        Class<?> aClass = commonClassLoader.loadClass(name, resolve);
        if (aClass == null) {
            aClass = super.loadClass(name, resolve);
        }
        return aClass;
    }
}
